package com.base.cn.platform.os.controller.course.comment;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.comment.CourseComment;
import com.base.cn.platform.os.entity.course.cou.buy.CourseBuyRecord;
import com.base.cn.platform.os.service.course.comment.CourseCommentBiz;
import com.base.cn.platform.os.service.course.cou.buy.CourseBuyRecordBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 课程评论
 *
 * @author SK
 * @since 2018-06-08
 */
@RestController
@RequestMapping("/course")
public class CourseCommentController extends BaseController {

    @Autowired
    private CourseBuyRecordBiz courseBuyRecordBiz;
    @Autowired
    private CourseCommentBiz courseCommentBiz;

    /**
     * 查找课程评论列表
     *
     * @param comment 课程评论
     * @return 课程评论列表
     */
    @RequestMapping("/findCourseCommentList")
    public List<CourseComment> findCourseCommentList(@RequestBody CourseComment comment,
                                                     @RequestParam("setCreator") boolean setCreator,
                                                     @RequestParam("setCourse") boolean setCourse,
                                                     @RequestParam("setCatalog") boolean setCatalog) {
        return courseCommentBiz.findCourseCommentList(comment, setCreator, setCourse, setCatalog);
    }

    /**
     * 查找课程评论列表
     *
     * @param comment 课程评论
     * @return 课程评论列表
     */
    @RequestMapping("/findCourseCommentPage")
    public PageInfo<CourseComment> findCourseCommentPage(@RequestBody CourseComment comment,
                                                         @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                                         @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                         @RequestParam("setCreator") boolean setCreator,
                                                         @RequestParam("setCourse") boolean setCourse,
                                                         @RequestParam("setCatalog") boolean setCatalog) {
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        if (DataUtil.isPositive(pageSize)) {
            pagination.setPageSize(pageSize);
        }
        return courseCommentBiz.findCourseCommentPage(comment, pagination, setCreator, setCourse, setCatalog);
    }

    /**
     * 保存课程评论
     *
     * @param comment 课程评论
     * @return 保存结果
     */
    @RequestMapping("/saveCourseComment")
    public Map<String, Object> saveCourseComment(@RequestBody CourseComment comment) {
        Map<String, Object> check = this.checkCourseComment(comment);
        if (Objects.nonNull(check)) {
            return check;
        }
        courseCommentBiz.saveCourseComment(comment);
        return ResultUtil.SUCCESS("反馈与评价成功");
    }

    /**
     * 去评价前，先做验证
     * @param comment 需要评价的课程数据
     * @return
     */
    @RequestMapping("/checkCourseCommentBefore")
    public Map<String, Object> checkCourseCommentBefore(@RequestBody CourseComment comment){
        Map<String, Object> check = this.checkCourseComment(comment);
        if (Objects.nonNull(check)) {
            return check;
        }
        return ResultUtil.SUCCESS();
    }

    /**
     * 更新课程评价状态
     *
     * @param commentIds 课程评价id
     * @param status     1.显示 2.隐藏
     * @return 保存结果
     */
    @RequestMapping("/updateCourseCommentStatus")
    public Map<String, Object> updateCourseCommentStatus(@RequestParam("commentIds") String commentIds,
                                                         @RequestParam("status") Integer status) {
        CourseComment comment = new CourseComment();
        comment.setStatus(status);
        String where = " id in (" + commentIds + ")";
        courseCommentBiz.updateByWhereSql(comment, where);
        return ResultUtil.SUCCESS("更新状态成功");
    }

    /**
     * 检查课程评论参数
     *
     * @param comment 课程评论
     */
    private Map<String, Object> checkCourseComment(CourseComment comment) {
        CourseBuyRecord record = courseBuyRecordBiz.checkUserBuyCourse(comment.getUserId(), comment.getCourseId());
        if (ObjectUtils.isEmpty(record)) {
            return ResultUtil.ERROR("未购买课程", Collections.singletonMap("code", 2)); // code 2 购买课程
        }
        if (!record.isActivated()) {
            return ResultUtil.ERROR("套餐未激活", Collections.singletonMap("code", 1)); // code 1 激活套餐
        }
        if(ObjectUtils.isNotEmpty(comment.getBefore())) {
            if (comment.getBefore()) {
                if (!DataUtil.idIsNotNull(comment.getCourseId()) ||
                        !DataUtil.isPositive(comment.getQuality()) ||
                        !DataUtil.isPositive(comment.getFeel()) ||
                        !DataUtil.isPositive(comment.getTeach())) {
                    return ResultUtil.ERROR("参数错误");
                }
            }
        }
        if (!DataUtil.idIsNotNull(comment.getUserId())) {
            return ResultUtil.ERROR("登录后才可以反馈哟");
        }
        if (!DataUtil.idIsNotNull(comment.getCatalogId())) {
            return ResultUtil.ERROR("只允许反馈正在学习的章节哟");
        }
        if(ObjectUtils.isNotEmpty(comment.getBefore())){
            if(comment.getBefore()){
                if (StringUtils.isEmpty(comment.getComment())) {
                    return ResultUtil.ERROR("填写反馈与评价内容");
                }
            }
        }
        CourseComment condition = new CourseComment();
        condition.setCatalogId(comment.getCatalogId());
        condition.setUserId(comment.getUserId());
        List<CourseComment> courseCommentList = courseCommentBiz.findCourseCommentList(comment, false, false, false);
        if (ObjectUtils.isNotEmpty(courseCommentList)) {
            return ResultUtil.ERROR("该章节已经评价过了");
        }
        return null;
    }

    /**
     *获取评论数
     * @param comment 查询条件
     * @return
     */
    @RequestMapping("/findCommentCount")
    public BigDecimal findCommentCount(@RequestBody CourseComment comment){
        return courseCommentBiz.findCommentCount(comment);
    }



}
